<template>
  <div class="container">
    <el-card>
      <!-- 表单区域 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="目录名称">
          <el-input v-model="formInline.directoryName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.state" placeholder="请选择">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="remove">清除</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" class="btn" @click="add">
            <i class="el-icon-edit"></i>新增目录
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 灰色数据数量 -->
      <div
        style="
          font-size: 13px;
          color: #909399;
          width: 100%;
          background-color: #f4f4f5;
          padding: 8px;
        "
      >
        <span style="line-height: 18px; border-radius: 5px">
          <i
            class="el-icon-info"
            style="margin-right: 5px; font-size: 15px"
          ></i>
          数据一共{{ count }}条
        </span>
      </div>
      <!-- 表格区域 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="subjectName" label="所属学科">
          <!-- <template slot-scope="scope"> -->
        </el-table-column>
        <el-table-column prop="directoryName" label="目录名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column label="创建日期">
          <template slot-scope="scope">
            {{ dayFormat(scope.row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        <el-table-column label="状态">
          <template v-slot="scope">
            {{ formatEmployment(scope.row.state) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 启用禁用按钮 -->
            <el-button type="text" size="mini" @click="handleEdit(scope.row)">
              {{ scope.row.state === 0 ? '启用' : '禁用' }}
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="editHandler(scope.row)"
              :disabled="Boolean(!scope.row.state)"
              >修改</el-button
            >
            <el-button
              :disabled="Boolean(!scope.row.state)"
              size="mini"
              type="text"
              @click="delHandler(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <div style="margin: 20px; text-align: right">
        <el-pagination
          :current-page="currentPage4"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pagTags.size"
          layout="  prev, pager, next,sizes, jumper"
          :total="count"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 弹框组件 -->
    <directorys
      ref="froms"
      :dialogAddVisible="dialogAddVisible"
      @Therefresh="accd"
      :tableData="tableData"
    ></directorys>
  </div>
</template>

<script>
import directorys from '@/module-hmmm/components/directorys-add.vue'
import { dateinit } from '@/utils/index.js'
import { list, changeState, remove } from '@/api/hmmm/directorys'
// changeState
import emp from '@/api/base/baseApi'
export default {
  components: {
    directorys
  },
  data () {
    return {
      dialogAddVisible: false,
      formInline: {
        directoryName: '',
        subjectID: null,
        state: 0
      },
      as: null,
      tableData: [],
      // 默认页
      currentPage4: 1,
      count: 0, // 数量
      pagTags: {
        page: 1,
        pagesize: 10
      }
    }
  },
  created () {
    this.formInline.state = null
    // this.subjectID = this.$route.query.id
    // this.directoryName = this.$route.query.name
    if (this.$route.query.id !== undefined) {
      this.formInline.subjectID = this.$route.query.id
      // this.formInline.directoryName = this.$route.query.name
      // this.getlist()
    }
    this.getlist()
  },
  methods: {
    // 修改
    editHandler (row) {
      // console.log(9999999999999999999)
      // console.log(row)
      // this.isEdit = true
      this.$refs.froms.ruleForm.id = row.id
      this.$refs.froms.dialogVisible = true
      this.$refs.froms.isall = true
      this.$refs.froms.ruleForm.directoryName = row.directoryName
      this.$refs.froms.ruleForm.subjectID = row.subjectID
    },
    // 删除
    async delHandler (id) {
      // console.log(id)
      // const res = await remove({ id: id })
      // if (res.status === 200) {
      //   this.accd()
      //   this.$message({
      //     message: '操作成功',
      //     type: 'success'
      //   })
      // }
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          console.log(id)
          const res = await remove({ id: id })
          console.log(res)
          this.accd()
        })
        .catch(() => {})
    },
    // 格式化时间
    dayFormat (time) {
      return dateinit(time)
    },
    // 清除
    remove () {
      this.formInline.directoryName = ''
      this.formInline.state = null
    },
    // 获取数据
    async getlist () {
      const { data: res } = await list(this.formInline)
      console.log(res)
      this.count = res.counts
      this.tableData = res.items
    },
    async accd () {
      const { data: res } = await list(this.pagTags)
      console.log(res)
      this.count = res.counts
      this.tableData = res.items
    },
    // 状态
    formatEmployment (num) {
      console.log(num)
      console.log(emp.sts)
      var obj = emp.sts.find((item) => item.id === num)
      console.log(obj)
      return obj.value
    },
    // 搜索
    async search () {
      // this.pagTags.page = 1
      this.isSerach = true
      if (this.formInline.directoryName === '') {
        this.formInline.subjectID = null
      }
      const { data: res } = await list(this.formInline)
      console.log(res)
      this.count = res.counts
      this.tableData = res.items
    },
    // 添加
    add () {
      this.$refs.froms.ruleForm.directoryName = ''
      this.$refs.froms.ruleForm.subjectID = ''
      this.$refs.froms.dialogVisible = true
      this.$refs.froms.isall = false
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)

      if (this.isSerach) {
        this.formInline.pagesize = val
        this.search()
      } else {
        this.pagTags.pagesize = val
        this.accd()
      }
    },
    // 当前页改变的时候触发
    handleCurrentChange (val) {
      // console.log(this.isSerach, '搜索值')
      if (this.isSerach) {
        // 搜索功能

        this.formInline.page = val
        this.search()
      } else {
        // console.log(`当前页: ${val}`)
        this.pagTags.page = val

        this.accd()
      }
    },
    // 启用和禁用
    async handleEdit (row) {
      this.formInline.subjectID = row.id
      if (row.state === 0) {
        this.as = 1
      } else {
        this.as = 0
      }
      const res = await changeState({
        id: this.formInline.subjectID,
        state: this.as
      })
      if (res.status === 200) {
        this.accd()
        this.$message({
          message: '操作成功',
          type: 'success'
        })
      }
    }
  }
}
</script>

<style scoped lang='scss'>
// .container {
//   padding: 20px 30px;
// }
::v-deep.btn {
  margin-left: 350px;
}
</style>
